package com.zhumr.web.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhumr.model.User;
import com.zhumr.service.IUserService;
import com.zhumr.util.SMSUtil;

/**
 * 登陆注册
 * 
 * @author HP
 *
 */
@Controller
@RequestMapping("/webUser")
public class UserController {

	@Autowired
	private IUserService userService;

	/**
	 * 登录
	 */
	@RequestMapping(value = "/weblogin", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getUser(HttpServletRequest request, String phone, String password) throws IOException {
		User user = new User();
		user.setUserName(phone);
		// user.setPasswordN(password);

		Map<String, Object> result = userService.selectUserByName(user, password);

		return result;
	}

	/**
	 * 获取验证码
	 * 
	 * @param request
	 * @param phone
	 * @throws IOException
	 */
	@RequestMapping("/webGetNumber")
	@ResponseBody
	public void getNumber(HttpServletRequest request, String phone) throws IOException {
		// 获取验证码
		String number = SMSUtil.get6Number();
		// 发送短信
		SMSUtil.sendSMS(phone, number);
		// 存入session
		HttpSession session = request.getSession();

		session.setAttribute(phone, number);
		// 半小时有效
		session.setMaxInactiveInterval(1300);
	}

	/**
	 * 注册
	 * 
	 * @param request
	 * @param phone
	 * @param number
	 * @param password
	 * @return
	 */
	@RequestMapping("/webRegistered")
	@ResponseBody
	public Map<String, Object> registered(HttpServletRequest request, String phone, String number, String password) {

		User user = new User();
		user.setUserName(phone);
		user.setPasswordN(password);
		user.setAddTime(new Date());
		user.setUpTime(new Date());

		HttpSession session = request.getSession();
		String sessionNumber = (String) session.getAttribute(phone);

		Map<String, Object> resultMap = new HashMap<String, Object>();

		if (!number.equals(sessionNumber)) {
			// 验证码错误
			resultMap.put("code", "1");
		} else {
			resultMap = userService.registered(user);
		}
		return resultMap;
	}

	/**
	 * 重置页面的验证码比对
	 * 
	 * @param request
	 * @param Phone
	 * @param number
	 * @return
	 */
	public Map<String, Object> resetVerification(HttpServletRequest request, String Phone, String number) {

		HttpSession session = request.getSession();
		String sessionNumber = (String) session.getAttribute(Phone);

		Map<String, Object> resultMap = new HashMap<String, Object>();

		if (!number.equals(sessionNumber)) {
			// 验证码错误
			resultMap.put("code", "1");
		} else {
			resultMap.put("code", "0");
		}

		return resultMap;
	}

	/**
	 * 重置密码
	 * 
	 * @param request
	 * @param phone
	 * @param password
	 * @return
	 */
	@RequestMapping("/webResetPass")
	@ResponseBody
	public Map<String, Object> resetPass(HttpServletRequest request, String phone, String password) {

		User user = new User();
		user.setUserName(phone);
		user.setPasswordN(password);
		user.setUpTime(new Date());

		Map<String, Object> resultMap = userService.resetPass(user);

		return resultMap;
	}

}
